<template>
    <!-- 表格组件 -->
    <el-table :data="tableData">
        <el-table-column width="180" label="日期">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <el-icon>
                        <timer />
                    </el-icon>
                    <span style="margin-left: 10px">{{ scope.row.article_create_time }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column width="180" label="名称" prop="article_title" />
        <el-table-column width="200" label="描述" prop="article_text" />
        <el-table-column width="80" label="作者">
            <template #default="scope">
                <span>{{ scope.row.article_author }}</span>
            </template>
        </el-table-column>
        <el-table-column label="封面">
            <template #default="scope">
                <a :href="scope.row.article_src" target="_blank"><img style="width:150px;height:60px" :src="scope.row.article_src" alt=""> </a>
            </template>
        </el-table-column>
        <el-table-column >
            <template #header>操作</template>
            <template #default="scope">
                <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button size="small" @click="handleDelete(scope.$index, scope.row)" type="danger">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div style="margin-top:20px">
        <el-pagination layout="prev, pager, next" :total="total2" @prev-click="handlePrevChange" @next-click="handleNextChange"  @current-change="handleCurrentChange"/>
    </div>
</template>

<script lang="ts" setup>
// 导入消息提示框
import { ElMessage } from 'element-plus';
import { reactive ,ref } from 'vue';
import { articleAllApi, articleItemsApi, articleRemoveApi } from '../../http/request';
import moment from "moment";
// 导入创建路由方法
import { useRouter } from 'vue-router';
// 定义路由
const $router = useRouter();
// 用户临时身份token
let token = localStorage.getItem('token');
// 定义数据结构
interface Article {
    article_create_time: string
    article_title: string
    article_src: string
    article_text: string
    article_author: string
    article_id: string
}
// 记录数据
let tableData: Article[] = reactive([]);
// 交互行为1
const handleEdit = (index: number, row: Article) => {
    // console.log('edit',index, row)
    $router.push({path:'/article/add',query:{article_id: row.article_id}})
}
// 交互行为2
const handleDelete = (index: number, row: Article) => {
    // 获取文章id
    let article_id = row.article_id;
    // console.log("图片ID:",article_id);
    if(confirm('您确定删除该文章吗?')){
         // 删除文章
        articleRemoveApi({ token, article_id})
            .then(
                (data: any) => {
                    if (data.code == 200) {
                        tableData.splice(index, 1)
                    }
                    // 提示
                    ElMessage({
                        message: data.msg,
                        type: 'warning',
                    });
                }
            )
    }
}
// 初始化文章列表
const initArticleItems = async (page_num:number|any, page_size:number|any) => {
    // 调用文章列表接口
    await articleItemsApi({page_num,page_size})
        .then(
            (data: any) => {
                if (data.code == 200) {
                    // console.log(data);
                    let { result } = data;
                    result.map((item: any) => {
                        if (item.article_create_time) {
                            item.article_create_time = moment(new Date(item.article_create_time)).format('YYYY-MM-DD hh:mm:ss')
                        }
                        tableData.push(item)
                    });
                }
            }
        )
}
// 初始化 
initArticleItems(1,10);

// 分页处理
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`)
  initArticleItems(val,10);
}
const handlePrevChange = (val: number) => {
  console.log(`prev page: ${val}`)
  initArticleItems(val,10);
}
const handleNextChange = (val: number) => {
  console.log(`next page: ${val}`)
  initArticleItems(val,10);
}



// 统计用户数量
let total2 = ref (50);
articleAllApi()
.then(
    data=>{
       if(data.code == 200){
          //console.log(data)
          total2.value = data.total;
       }
    }
)
</script>